<template>
  <div class="app-container">
    <div class="filter-container">
      <!--      <el-select v-model="listQuery.room_id" clearable placeholder="宿舍号" class="filter-item">-->
      <!--        <el-option v-for="(value, index) in room" :key="index" :value="index" :label="value"/>-->
      <!--      </el-select>-->
      <!--      <el-button type="primary" class="filter-item" @click="handleFilter">搜索</el-button>-->
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border highlight-current-row>
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-table :data="scope.row.tizi_liuyan" border>
            <el-table-column prop="student_no" label="学号" width="180"/>
            <el-table-column prop="username" label="回帖人" width="180"/>
            <el-table-column prop="title" label="内容"/>
            <el-table-column prop="create_time" label="回帖时间" width="180"/>
            <el-table-column label="操作" width="180">
              <template slot-scope="scope">
                <el-button type="danger" size="mini" @click="handleDeleteLiuYan(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="ID">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="标题">
        <template slot-scope="scope">{{ scope.row.title }}</template>
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.image_url" style="max-height: 100%;max-width: 100%;">
        </template>
      </el-table-column>
      <el-table-column label="学号">
        <template slot-scope="scope">{{ scope.row.student_no }}</template>
      </el-table-column>
      <el-table-column label="发帖人">
        <template slot-scope="scope">{{ scope.row.username }}</template>
      </el-table-column>
      <el-table-column label="发帖时间">
        <template slot-scope="scope">{{ scope.row.create_time }}</template>
      </el-table-column>
      <el-table-column label="是否显示">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" active-color="#13ce66" inactive-color="#ff4949" @change="handleSwitch(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <router-link :to="{path:'/tiezi/detail',query:{id:scope.row.id}}">
            <el-button type="primary" size="mini">编辑</el-button>
          </router-link>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData"/>
  </div>
</template>

<script>
import { getList, del, delLiuyan, changeStatus } from '@/api/tiezi'
import Pagination from '@/components/Pagination'
export default {
  name: 'Index',
  components: { Pagination },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 20
      },
      listLoading: true,
      list: [],
      total: 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getList(this.listQuery).then(res => {
        this.list = res.data.list
        this.total = res.data.total
        this.listLoading = false
      })
    },
    handleDeleteLiuYan(id) {
      this.$confirm('此操作将永久删除 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delLiuyan({ id: id }).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          message: '取消删除',
          type: 'info'
        })
      })
    },
    handleDelete(id) {
      this.$confirm('是否删除帖子及其留言贴？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del({ id: id }).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          message: '取消删除',
          type: 'info'
        })
      })
    },
    handleSwitch(row) {
      const data = { id: row.id, status: row.status }
      this.$confirm('是否改变状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        changeStatus(data).then(response => {
          this.$notify({
            title: '成功',
            message: '状态修改成功',
            type: 'success',
            duration: 2000
          })
        })
      }).catch(() => {
        row.status = ~~!row.status
      })
    }
  }
}
</script>

<style scoped>

</style>
